<html><head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
   <title>22.&nbsp;JMX</title><link rel="stylesheet" href="css/stylesheet.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.74.0"><link rel="home" href="index.html" title="Reference Documentation"><link rel="up" href="spring-integration.html" title="Part&nbsp;VI.&nbsp;Integration"><link rel="prev" href="jms.html" title="21.&nbsp;JMS (Java Message Service)"><link rel="next" href="cci.html" title="23.&nbsp;JCA CCI"><!--Begin Google Analytics code--><script type="text/javascript">
			var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
			document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
		</script><script type="text/javascript">
			var pageTracker = _gat._getTracker("UA-2728886-3");
			pageTracker._setDomainName("none");
			pageTracker._setAllowLinker(true);
			pageTracker._trackPageview();
		</script><!--End Google Analytics code--></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">22.&nbsp;JMX</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="jms.html">Prev</a>&nbsp;</td><th width="60%" align="center">Part&nbsp;VI.&nbsp;Integration</th><td width="20%" align="right">&nbsp;<a accesskey="n" href="cci.html">Next</a></td></tr></table><hr></div><div class="chapter" lang="en"><div class="titlepage"><div><div><h2 class="title"><a name="jmx"></a>22.&nbsp;JMX</h2></div></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="jmx-introduction"></a>22.1&nbsp;Introduction</h2></div></div></div><p>The JMX support in Spring provides you with the features to easily
    and transparently integrate your Spring application into a JMX
    infrastructure.</p><div class="sidebar"><p class="title"><b>JMX?</b></p><p>This chapter is not an introduction to JMX... it doesn't try to
      explain the motivations of why one might want to use JMX (or indeed what
      the letters JMX actually stand for). If you are new to JMX, check out
      <a class="xref" href="jmx.html#jmx-resources" title="22.8&nbsp;Further Resources">Section&nbsp;22.8, &#8220;Further Resources&#8221;</a> at the end of this chapter.</p></div><p>Specifically, Spring's JMX support provides four core
    features:</p><div class="itemizedlist"><ul type="disc"><li><p>The automatic registration of <span class="emphasis"><em>any</em></span> Spring
        bean as a JMX MBean</p></li><li><p>A flexible mechanism for controlling the management interface of
        your beans</p></li><li><p>The declarative exposure of MBeans over remote, JSR-160
        connectors</p></li><li><p>The simple proxying of both local and remote MBean
        resources</p></li></ul></div><p>These features are designed to work without coupling your
    application components to either Spring or JMX interfaces and classes.
    Indeed, for the most part your application classes need not be aware of
    either Spring or JMX in order to take advantage of the Spring JMX
    features.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="jmx-exporting"></a>22.2&nbsp;Exporting your beans to JMX</h2></div></div></div><p>The core class in Spring's JMX framework is the
    <code class="classname">MBeanExporter</code>. This class is responsible for taking
    your Spring beans and registering them with a JMX
    <code class="interfacename">MBeanServer</code>. For example, consider the following
    class:</p><pre class="programlisting"><span class="hl-keyword">package</span> org.springframework.jmx;

<span class="hl-keyword">public</span> <span class="hl-keyword">class</span> JmxTestBean <span class="hl-keyword">implements</span> IJmxTestBean {

    <span class="hl-keyword">private</span> String name;
    <span class="hl-keyword">private</span> <span class="hl-keyword">int</span> age;
    <span class="hl-keyword">private</span> <span class="hl-keyword">boolean</span> isSuperman;

    <span class="hl-keyword">public</span> <span class="hl-keyword">int</span> getAge() {
        <span class="hl-keyword">return</span> age;
    }

    <span class="hl-keyword">public</span> <span class="hl-keyword">void</span> setAge(<span class="hl-keyword">int</span> age) {
        <span class="hl-keyword">this</span>.age = age;
    }
    
    <span class="hl-keyword">public</span> <span class="hl-keyword">void</span> setName(String name) {
        <span class="hl-keyword">this</span>.name = name;
    }

    <span class="hl-keyword">public</span> String getName() {
        <span class="hl-keyword">return</span> name;
    }

    <span class="hl-keyword">public</span> <span class="hl-keyword">int</span> add(<span class="hl-keyword">int</span> x, <span class="hl-keyword">int</span> y) {
        <span class="hl-keyword">return</span> x + y;
    }

    <span class="hl-keyword">public</span> <span class="hl-keyword">void</span> dontExposeMe() {
        <span class="hl-keyword">throw</span> <span class="hl-keyword">new</span> RuntimeException();
    }
}</pre><p>To expose the properties and methods of this bean as attributes and
    operations of an MBean you simply configure an instance of the
    <code class="classname">MBeanExporter</code> class in your configuration file and
    pass in the bean as shown below:</p><pre class="programlisting">&lt;<span class="hl-tag">beans</span>&gt;

  &lt;<span class="hl-comment">!-- this bean must not be lazily initialized if the exporting is to happen --</span>&gt;
  &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"exporter"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.export.MBeanExporter"</span> <span class="hl-attribute">lazy-init</span>=<span class="hl-value">"false"</span>&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"beans"</span>&gt;
      &lt;<span class="hl-tag">map</span>&gt;
        &lt;<span class="hl-tag">entry</span> <span class="hl-attribute">key</span>=<span class="hl-value">"bean:name=testBean1"</span> <span class="hl-attribute">value-ref</span>=<span class="hl-value">"testBean"</span>/&gt;
      &lt;<span class="hl-tag">/map</span>&gt;
    &lt;<span class="hl-tag">/property</span>&gt;
  &lt;<span class="hl-tag">/bean</span>&gt;

  &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"testBean"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.JmxTestBean"</span>&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"name"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"TEST"</span>/&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"age"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"100"</span>/&gt;
  &lt;<span class="hl-tag">/bean</span>&gt;

&lt;<span class="hl-tag">/beans</span>&gt;</pre><p>The pertinent bean definition from the above configuration snippet
    is the <code class="literal">exporter</code> bean. The <code class="literal">beans</code>
    property tells the <code class="classname">MBeanExporter</code> exactly which of
    your beans must be exported to the JMX <code class="interfacename">MBeanServer</code>.
    In the default configuration, the key of each entry in the
    <code class="literal">beans</code> <code class="interfacename">Map</code> is used as the
    <code class="classname">ObjectName</code> for the bean referenced by the
    corresponding entry value. This behavior can be changed as described in
    <a class="xref" href="jmx.html#jmx-naming" title="22.4&nbsp;Controlling the ObjectNames for your beans">Section&nbsp;22.4, &#8220;Controlling the ObjectNames for your beans&#8221;</a>.</p><p>With this configuration the <code class="literal">testBean</code> bean is
    exposed as an MBean under the <code class="classname">ObjectName</code>
    <code class="literal">bean:name=testBean1</code>. By default, all
    <span class="emphasis"><em>public</em></span> properties of the bean are exposed as
    attributes and all <span class="emphasis"><em>public</em></span> methods (bar those
    inherited from the <code class="classname">Object</code> class) are exposed as
    operations.</p><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="jmx-exporting-mbeanserver"></a>22.2.1&nbsp;Creating an <code class="interfacename">MBeanServer</code></h3></div></div></div><p>The above configuration assumes that the application is running in
      an environment that has one (and only one)
      <code class="interfacename">MBeanServer</code> already running. In this case, Spring
      will attempt to locate the running <code class="interfacename">MBeanServer</code>
      and register your beans with that server (if any). This behavior is
      useful when your application is running inside a container such as
      Tomcat or IBM WebSphere that has its own
      <code class="interfacename">MBeanServer</code>.</p><p>However, this approach is of no use in a standalone environment,
      or when running inside a container that does not provide an
      <code class="interfacename">MBeanServer</code>. To address this you can create an
      <code class="interfacename">MBeanServer</code> instance declaratively by adding an
      instance of the
      <code class="classname">org.springframework.jmx.support.MBeanServerFactoryBean</code>
      class to your configuration. You can also ensure that a specific
      <code class="interfacename">MBeanServer</code> is used by setting the value of the
      <code class="classname">MBeanExporter</code>'s <code class="literal">server</code>
      property to the <code class="interfacename">MBeanServer</code> value returned by an
      <code class="classname">MBeanServerFactoryBean</code>; for example:</p><pre class="programlisting">&lt;<span class="hl-tag">beans</span>&gt;

  &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"mbeanServer"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.support.MBeanServerFactoryBean"</span>/&gt;

  &lt;<span class="hl-comment">!--
    this bean needs to be eagerly pre-instantiated in order for the exporting to occur;
    this means that it must not be marked as lazily initialized
  --</span>&gt;
  &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"exporter"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.export.MBeanExporter"</span>&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"beans"</span>&gt;
      &lt;<span class="hl-tag">map</span>&gt;
        &lt;<span class="hl-tag">entry</span> <span class="hl-attribute">key</span>=<span class="hl-value">"bean:name=testBean1"</span> <span class="hl-attribute">value-ref</span>=<span class="hl-value">"testBean"</span>/&gt;
      &lt;<span class="hl-tag">/map</span>&gt;
    &lt;<span class="hl-tag">/property</span>&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"server"</span> <span class="hl-attribute">ref</span>=<span class="hl-value">"mbeanServer"</span>/&gt;
  &lt;<span class="hl-tag">/bean</span>&gt;

  &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"testBean"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.JmxTestBean"</span>&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"name"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"TEST"</span>/&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"age"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"100"</span>/&gt;
  &lt;<span class="hl-tag">/bean</span>&gt;

&lt;<span class="hl-tag">/beans</span>&gt;</pre><p>Here an instance of <code class="interfacename">MBeanServer</code> is created
      by the <code class="classname">MBeanServerFactoryBean</code> and is supplied to
      the <code class="classname">MBeanExporter</code> via the server property. When
      you supply your own <code class="interfacename">MBeanServer</code> instance, the
      <code class="classname">MBeanExporter</code> will not attempt to locate a
      running <code class="interfacename">MBeanServer</code> and will use the supplied
      <code class="interfacename">MBeanServer</code> instance. For this to work correctly,
      you must (of course) have a JMX implementation on your classpath.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="jmx-mbean-server"></a>22.2.2&nbsp;Reusing an existing <code class="interfacename">MBeanServer</code></h3></div></div></div><p>If no server is specified, the <code class="classname">MBeanExporter</code>
      tries to automatically detect a running <code class="interfacename">MBeanServer</code>.
      This works in most environment where only one
      <code class="interfacename">MBeanServer</code> instance is used, however when multiple
      instances exist, the exporter might pick the wrong server. In such
      cases, one should use the <code class="interfacename">MBeanServer</code>
      <code class="literal">agentId</code> to indicate which instance to be used:</p><pre class="programlisting">&lt;<span class="hl-tag">beans</span>&gt;
   &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"mbeanServer"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.support.MBeanServerFactoryBean"</span>&gt;
     &lt;<span class="hl-comment">!-- indicate to first look for a server --</span>&gt;
     &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"locateExistingServerIfPossible"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"true"</span>/&gt;
     &lt;<span class="hl-comment">!-- search for the MBeanServer instance with the given agentId --</span>&gt;
     &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"agentId"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"&lt;MBeanServer instance agentId&gt;"</span>/&gt;
   &lt;<span class="hl-tag">/bean</span>&gt;
   
   &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"exporter"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.export.MBeanExporter"</span>&gt;
     &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"server"</span> <span class="hl-attribute">ref</span>=<span class="hl-value">"mbeanServer"</span>/&gt;
   ...
   &lt;<span class="hl-tag">/bean</span>&gt;
&lt;<span class="hl-tag">/beans</span>&gt;</pre><p>For platforms/cases where the existing <code class="interfacename">MBeanServer</code>
      has a dynamic (or unknown) <code class="literal">agentId</code> which is retrieved through lookup
      methods, one should use <a class="link" href="beans.html#beans-factory-class-static-factory-method" title="3.3.2.2&nbsp;Instantiation with a static factory method">factory-method</a>:</p><pre class="programlisting">&lt;<span class="hl-tag">beans</span>&gt;
   &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"exporter"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.export.MBeanExporter"</span>&gt;
     &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"server"</span>&gt;
       &lt;<span class="hl-comment">!-- Custom MBeanServerLocator --</span>&gt;
       &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">class</span>=<span class="hl-value">"platform.package.MBeanServerLocator"</span> <span class="hl-attribute">factory-method</span>=<span class="hl-value">"locateMBeanServer"</span>/&gt;
     &lt;<span class="hl-tag">/property</span>&gt;
     
     &lt;<span class="hl-comment">!-- other beans here --</span>&gt;
     
   &lt;<span class="hl-tag">/bean</span>&gt;
&lt;<span class="hl-tag">/beans</span>&gt;</pre></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="jmx-exporting-lazy"></a>22.2.3&nbsp;Lazy-initialized MBeans</h3></div></div></div><p>If you configure a bean with the
      <code class="classname">MBeanExporter</code> that is also configured for lazy
      initialization, then the <code class="classname">MBeanExporter</code> will
      <span class="bold"><strong>not</strong></span> break this contract and will avoid
      instantiating the bean. Instead, it will register a proxy with
      the <code class="interfacename">MBeanServer</code> and will defer obtaining the bean
      from the container until the first invocation on the proxy occurs.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="jmx-exporting-auto"></a>22.2.4&nbsp;Automatic registration of MBeans</h3></div></div></div><p>Any beans that are exported through the
      <code class="classname">MBeanExporter</code> and are already valid MBeans are
      registered as-is with the <code class="interfacename">MBeanServer</code> without
      further intervention from Spring. MBeans can be automatically detected
      by the <code class="classname">MBeanExporter</code> by setting the
      <code class="literal">autodetect</code> property to <code class="literal">true</code>:</p><pre class="programlisting">&lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"exporter"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.export.MBeanExporter"</span>&gt;
  &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"autodetect"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"true"</span>/&gt;
&lt;<span class="hl-tag">/bean</span>&gt;

&lt;<span class="hl-tag">bean</span> <span class="hl-attribute">name</span>=<span class="hl-value">"spring:mbean=true"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.export.TestDynamicMBean"</span>/&gt;</pre><p>Here, the bean called <code class="literal">spring:mbean=true</code> is
      already a valid JMX MBean and will be automatically registered by
      Spring. By default, beans that are autodetected for JMX registration
      have their bean name used as the <code class="classname">ObjectName</code>. This
      behavior can be overridden as detailed in <a class="xref" href="jmx.html#jmx-naming" title="22.4&nbsp;Controlling the ObjectNames for your beans">Section&nbsp;22.4, &#8220;Controlling the ObjectNames for your beans&#8221;</a>.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="jmx-exporting-registration-behavior"></a>22.2.5&nbsp;Controlling the registration behavior</h3></div></div></div><p>Consider the scenario where a Spring
      <code class="classname">MBeanExporter</code> attempts to register an
      <code class="classname">MBean</code> with an <code class="interfacename">MBeanServer</code>
      using the <code class="classname">ObjectName</code>
      <code class="literal">'bean:name=testBean1'</code>. If an
      <code class="classname">MBean</code> instance has already been registered under
      that same <code class="classname">ObjectName</code>, the default behavior is to
      fail (and throw an
      <code class="exceptionname">InstanceAlreadyExistsException</code>).</p><p>It is possible to control the behavior of exactly what happens
      when an <code class="classname">MBean</code> is registered with an
      <code class="interfacename">MBeanServer</code>. Spring's JMX support allows for
      three different registration behaviors to control the registration
      behavior when the registration process finds that an
      <code class="classname">MBean</code> has already been registered under the same
      <code class="classname">ObjectName</code>; these registration behaviors are
      summarized on the following table:</p><div class="table"><a name="jmx-registration-behaviors"></a><p class="title"><b>Table&nbsp;22.1.&nbsp;Registration Behaviors</b></p><div class="table-contents"><table summary="Registration Behaviors" style="border-collapse: collapse;border-top: 1.0pt solid ; border-bottom: 1.0pt solid ; border-left: 1.0pt solid ; border-right: 1.0pt solid ; "><colgroup><col align="left"><col></colgroup><thead><tr><th style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; " align="center">Registration behavior</th><th style="border-bottom: 1.0pt solid ; " align="center">Explanation</th></tr></thead><tbody><tr><td style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; " align="left"><p><code class="literal">REGISTRATION_FAIL_ON_EXISTING</code></p></td><td style="border-bottom: 1.0pt solid ; "><p> This is the default registration behavior. If an
                <code class="classname">MBean</code> instance has already been
                registered under the same <code class="classname">ObjectName</code>,
                the <code class="classname">MBean</code> that is being registered will
                not be registered and an
                <code class="exceptionname">InstanceAlreadyExistsException</code> will be
                thrown. The existing <code class="classname">MBean</code> is
                unaffected. </p></td></tr><tr><td style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; " align="left"><p><code class="literal">REGISTRATION_IGNORE_EXISTING</code></p></td><td style="border-bottom: 1.0pt solid ; "><p> If an <code class="classname">MBean</code> instance has
                already been registered under the same
                <code class="classname">ObjectName</code>, the
                <code class="classname">MBean</code> that is being registered will
                <span class="emphasis"><em>not</em></span> be registered. The existing
                <code class="classname">MBean</code> is unaffected, and no
                <code class="exceptionname">Exception</code> will be thrown. </p>
                <p> This is useful in settings where multiple applications
                want to share a common <code class="classname">MBean</code> in a
                shared <code class="interfacename">MBeanServer</code>. </p></td></tr><tr><td style="border-right: 1.0pt solid ; " align="left"><p><code class="literal">REGISTRATION_REPLACE_EXISTING</code></p></td><td style=""><p> If an <code class="classname">MBean</code> instance has
                already been registered under the same
                <code class="classname">ObjectName</code>, the existing
                <code class="classname">MBean</code> that was previously registered
                will be unregistered and the new <code class="classname">MBean</code>
                will be registered in its place (the new
                <code class="classname">MBean</code> effectively replaces the previous
                instance). </p></td></tr></tbody></table></div></div><br class="table-break"><p>The above values are defined as constants on the
      <code class="classname">MBeanRegistrationSupport</code> class (the
      <code class="classname">MBeanExporter</code> class derives from this
      superclass). If you want to change the default registration behavior,
      you simply need to set the value of the
      <code class="literal">registrationBehaviorName</code> property on your
      <code class="classname">MBeanExporter</code> definition to one of those
      values.</p><p>The following example illustrates how to effect a change from the
      default registration behavior to the
      <code class="literal">REGISTRATION_REPLACE_EXISTING</code> behavior:</p><pre class="programlisting">&lt;<span class="hl-tag">beans</span>&gt;

    &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"exporter"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.export.MBeanExporter"</span>&gt;
        &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"beans"</span>&gt;
            &lt;<span class="hl-tag">map</span>&gt;
                &lt;<span class="hl-tag">entry</span> <span class="hl-attribute">key</span>=<span class="hl-value">"bean:name=testBean1"</span> <span class="hl-attribute">value-ref</span>=<span class="hl-value">"testBean"</span>/&gt;
            &lt;<span class="hl-tag">/map</span>&gt;
        &lt;<span class="hl-tag">/property</span>&gt;
        &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"registrationBehaviorName"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"REGISTRATION_REPLACE_EXISTING"</span>/&gt;
    &lt;<span class="hl-tag">/bean</span>&gt;

    &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"testBean"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.JmxTestBean"</span>&gt;
        &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"name"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"TEST"</span>/&gt;
        &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"age"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"100"</span>/&gt;
    &lt;<span class="hl-tag">/bean</span>&gt;

&lt;<span class="hl-tag">/beans</span>&gt;</pre></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="jmx-interface"></a>22.3&nbsp;Controlling the management interface of your beans</h2></div></div></div><p>In the previous example, you had little control over the management
    interface of your bean; <span class="emphasis"><em>all</em></span> of the
    <span class="emphasis"><em>public</em></span> properties and methods of each exported bean
    was exposed as JMX attributes and operations respectively. To exercise
    finer-grained control over exactly which properties and methods of your
    exported beans are actually exposed as JMX attributes and operations,
    Spring JMX provides a comprehensive and extensible mechanism for
    controlling the management interfaces of your beans.</p><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="jmx-interface-assembler"></a>22.3.1&nbsp;The <code class="interfacename">MBeanInfoAssembler</code>
      Interface</h3></div></div></div><p>Behind the scenes, the <code class="classname">MBeanExporter</code>
      delegates to an implementation of the
      <code class="classname">org.springframework.jmx.export.assembler.MBeanInfoAssembler</code>
      interface which is responsible for defining the management interface of
      each bean that is being exposed. The default implementation,
      <code class="classname">org.springframework.jmx.export.assembler.SimpleReflectiveMBeanInfoAssembler</code>,
      simply defines a management interface that exposes all public properties
      and methods (as you saw in the previous examples). Spring provides two
      additional implementations of the
      <code class="interfacename">MBeanInfoAssembler</code> interface that allow
      you to control the generated management interface using either
      source-level metadata or any arbitrary interface.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="jmx-interface-metadata"></a>22.3.2&nbsp;Using Source-Level Metadata (JDK 5.0 annotations)</h3></div></div></div><p>Using the <code class="classname">MetadataMBeanInfoAssembler</code> you
      can define the management interfaces for your beans using source level
      metadata. The reading of metadata is encapsulated by the
      <code class="classname">org.springframework.jmx.export.metadata.JmxAttributeSource</code>
      interface. Spring JMX provides a default implementation which uses JDK 5.0 annotations, namely
      <code class="classname">org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource</code>. The
      <code class="classname">MetadataMBeanInfoAssembler</code>
      <span class="emphasis"><em>must</em></span> be configured with an implementation instance
      of the <code class="classname">JmxAttributeSource</code> interface for it to
      function correctly (there is <span class="emphasis"><em>no</em></span> default).</p><p>To mark a bean for export to JMX, you should annotate the bean
      class with the <code class="classname">ManagedResource</code> annotation. Each
      method you wish to expose as an operation must be marked with the
      <code class="classname">ManagedOperation</code> annotation and each property you
      wish to expose must be marked with the
      <code class="classname">ManagedAttribute</code> annotation. When marking
      properties you can omit either the annotation of the getter or the
      setter to create a write-only or read-only attribute
      respectively.</p><p>The example below shows the annotated version of the 
        <code class="classname">JmxTestBean</code> class that you saw earlier:</p><pre class="programlisting"><span class="hl-keyword">package</span> org.springframework.jmx;

<span class="hl-keyword">import</span> org.springframework.jmx.export.annotation.ManagedResource;
<span class="hl-keyword">import</span> org.springframework.jmx.export.annotation.ManagedOperation;
<span class="hl-keyword">import</span> org.springframework.jmx.export.annotation.ManagedAttribute;

@ManagedResource(objectName=<span class="hl-string">"bean:name=testBean4"</span>, description=<span class="hl-string">"My Managed Bean"</span>, log=true,
    logFile=<span class="hl-string">"jmx.log"</span>, currencyTimeLimit=15, persistPolicy=<span class="hl-string">"OnUpdate"</span>, persistPeriod=200,
    persistLocation=<span class="hl-string">"foo"</span>, persistName=<span class="hl-string">"bar"</span>)
<span class="hl-keyword">public</span> <span class="hl-keyword">class</span> AnnotationTestBean <span class="hl-keyword">implements</span> IJmxTestBean {

  <span class="hl-keyword">private</span> String name;
  <span class="hl-keyword">private</span> <span class="hl-keyword">int</span> age;

  @ManagedAttribute(description=<span class="hl-string">"The Age Attribute"</span>, currencyTimeLimit=15)
  <span class="hl-keyword">public</span> <span class="hl-keyword">int</span> getAge() {
    <span class="hl-keyword">return</span> age;
  }

  <span class="hl-keyword">public</span> <span class="hl-keyword">void</span> setAge(<span class="hl-keyword">int</span> age) {
    <span class="hl-keyword">this</span>.age = age;
  }

  @ManagedAttribute(description=<span class="hl-string">"The Name Attribute"</span>,
      currencyTimeLimit=20,
      defaultValue=<span class="hl-string">"bar"</span>,
      persistPolicy=<span class="hl-string">"OnUpdate"</span>)
  <span class="hl-keyword">public</span> <span class="hl-keyword">void</span> setName(String name) {
    <span class="hl-keyword">this</span>.name = name;
  }

  @ManagedAttribute(defaultValue=<span class="hl-string">"foo"</span>, persistPeriod=300)
  <span class="hl-keyword">public</span> String getName() {
    <span class="hl-keyword">return</span> name;
  }

  @ManagedOperation(description=<span class="hl-string">"Add two numbers"</span>)
  @ManagedOperationParameters({
    @ManagedOperationParameter(name = <span class="hl-string">"x"</span>, description = <span class="hl-string">"The first number"</span>),
    @ManagedOperationParameter(name = <span class="hl-string">"y"</span>, description = <span class="hl-string">"The second number"</span>)})
  <span class="hl-keyword">public</span> <span class="hl-keyword">int</span> add(<span class="hl-keyword">int</span> x, <span class="hl-keyword">int</span> y) {
    <span class="hl-keyword">return</span> x + y;
  }

  <span class="hl-keyword">public</span> <span class="hl-keyword">void</span> dontExposeMe() {
    <span class="hl-keyword">throw</span> <span class="hl-keyword">new</span> RuntimeException();
  }
}</pre><p>Here you can see that the <code class="classname">JmxTestBean</code> class
      is marked with the <code class="classname">ManagedResource</code> annotation and
        that this <code class="classname">ManagedResource</code> annotation is configured
      with a set of properties. These properties can be used to configure
      various aspects of the MBean that is generated by the
      <code class="classname">MBeanExporter</code>, and are explained in greater
      detail later in section entitled <a class="xref" href="jmx.html#jmx-interface-metadata-types" title="22.3.3&nbsp;Source-Level Metadata Types">Section&nbsp;22.3.3, &#8220;Source-Level Metadata Types&#8221;</a>.</p><p>You will also notice that both the <code class="literal">age</code> and
      <code class="literal">name</code> properties are annotated with the
      <code class="classname">ManagedAttribute</code> annotation, but in the case of
      the <code class="literal">age</code> property, only the getter is marked. This
      will cause both of these properties to be included in the management
      interface as attributes, but the <code class="literal">age</code> attribute will
      be read-only.</p><p>Finally, you will notice that the <code class="literal">add(int, int)</code>
      method is marked with the <code class="classname">ManagedOperation</code>
      attribute whereas the <code class="literal">dontExposeMe()</code> method is not.
      This will cause the management interface to contain only one operation,
      <code class="literal">add(int, int)</code>, when using the
      <code class="classname">MetadataMBeanInfoAssembler</code>.</p><p>The configuration below shows how you configure the
      <code class="classname">MBeanExporter</code> to use the
      <code class="classname">MetadataMBeanInfoAssembler</code>:</p><pre class="programlisting">&lt;<span class="hl-tag">beans</span>&gt;
    &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"exporter"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.export.MBeanExporter"</span>&gt;
        &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"assembler"</span> <span class="hl-attribute">ref</span>=<span class="hl-value">"assembler"</span>/&gt;
        &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"namingStrategy"</span> <span class="hl-attribute">ref</span>=<span class="hl-value">"namingStrategy"</span>/&gt;
        &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"autodetect"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"true"</span>/&gt;
    &lt;<span class="hl-tag">/bean</span>&gt;

    &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"jmxAttributeSource"</span>
          <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource"</span>/&gt;

    &lt;<span class="hl-comment">!-- will create management interface using annotation metadata --</span>&gt;
    &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"assembler"</span>
          <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler"</span>&gt;
        &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"attributeSource"</span> <span class="hl-attribute">ref</span>=<span class="hl-value">"jmxAttributeSource"</span>/&gt;
    &lt;<span class="hl-tag">/bean</span>&gt;

    &lt;<span class="hl-comment">!-- will pick up the ObjectName from the annotation --</span>&gt;
    &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"namingStrategy"</span>
          <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.export.naming.MetadataNamingStrategy"</span>&gt;
        &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"attributeSource"</span> <span class="hl-attribute">ref</span>=<span class="hl-value">"jmxAttributeSource"</span>/&gt;
    &lt;<span class="hl-tag">/bean</span>&gt;

    &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"testBean"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.AnnotationTestBean"</span>&gt;
        &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"name"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"TEST"</span>/&gt;
        &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"age"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"100"</span>/&gt;
    &lt;<span class="hl-tag">/bean</span>&gt;
&lt;<span class="hl-tag">/beans</span>&gt;</pre><p>Here you can see that an
      <code class="classname">MetadataMBeanInfoAssembler</code> bean has been
      configured with an instance of the
        <code class="classname">AnnotationJmxAttributeSource</code> class and passed to
      the <code class="classname">MBeanExporter</code> through the assembler property.
      This is all that is required to take advantage of metadata-driven
      management interfaces for your Spring-exposed MBeans.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="jmx-interface-metadata-types"></a>22.3.3&nbsp;Source-Level Metadata Types</h3></div></div></div><p>The following source level metadata types are available for use in
      Spring JMX:</p><div class="table"><a name="jmx-metadata-types"></a><p class="title"><b>Table&nbsp;22.2.&nbsp;Source-Level Metadata Types</b></p><div class="table-contents"><table summary="Source-Level Metadata Types" style="border-collapse: collapse;border-top: 1.0pt solid ; border-bottom: 1.0pt solid ; border-left: 1.0pt solid ; border-right: 1.0pt solid ; "><colgroup><col align="left"><col><col></colgroup><thead><tr><th style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; " align="center">Purpose</th><th style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; " align="center">Annotation</th><th style="border-bottom: 1.0pt solid ; " align="center">Annotation Type</th></tr></thead><tbody><tr><td style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; " align="left">Mark all instances of a <code class="classname">Class</code> as
                JMX managed resources</td><td style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; "><code class="literal">@ManagedResource</code></td><td style="border-bottom: 1.0pt solid ; ">Class</td></tr><tr><td style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; " align="left">Mark a method as a JMX operation</td><td style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; "><code class="literal">@ManagedOperation</code></td><td style="border-bottom: 1.0pt solid ; ">Method</td></tr><tr><td style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; " align="left">Mark a getter or setter as one half of a JMX
                attribute</td><td style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; "><code class="classname">@ManagedAttribute</code></td><td style="border-bottom: 1.0pt solid ; ">Method (only getters and setters)</td></tr><tr><td style="border-right: 1.0pt solid ; " align="left">Define descriptions for operation parameters</td><td style="border-right: 1.0pt solid ; "><code class="classname">@ManagedOperationParameter</code> and
                <code class="classname">@ManagedOperationParameters</code></td><td style="">Method</td></tr></tbody></table></div></div><p><br class="table-break"></p><p>The following configuration parameters are available for use on
      these source-level metadata types:</p><div class="table"><a name="jmx-metadata-parameters"></a><p class="title"><b>Table&nbsp;22.3.&nbsp;Source-Level Metadata Parameters</b></p><div class="table-contents"><table summary="Source-Level Metadata Parameters" style="border-collapse: collapse;border-top: 1.0pt solid ; border-bottom: 1.0pt solid ; border-left: 1.0pt solid ; border-right: 1.0pt solid ; "><colgroup><col align="left"><col><col></colgroup><thead><tr><th style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; " align="center">Parameter</th><th style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; " align="center">Description</th><th style="border-bottom: 1.0pt solid ; " align="center">Applies to</th></tr></thead><tbody><tr><td style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; " align="left"><code class="classname">ObjectName</code></td><td style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; ">Used by <code class="classname">MetadataNamingStrategy</code>
                to determine the <code class="classname">ObjectName</code> of a
                managed resource</td><td style="border-bottom: 1.0pt solid ; "><code class="classname">ManagedResource</code></td></tr><tr><td style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; " align="left"><code class="literal">description</code></td><td style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; ">Sets the friendly description of the resource,
                attribute or operation</td><td style="border-bottom: 1.0pt solid ; "><code class="classname">ManagedResource</code>,
                <code class="classname">ManagedAttribute</code>,
                <code class="classname">ManagedOperation</code>,
                <code class="classname">ManagedOperationParameter</code></td></tr><tr><td style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; " align="left"><code class="literal">currencyTimeLimit</code></td><td style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; ">Sets the value of the
                <code class="literal">currencyTimeLimit</code> descriptor field</td><td style="border-bottom: 1.0pt solid ; "><code class="classname">ManagedResource</code>,
                <code class="classname">ManagedAttribute</code></td></tr><tr><td style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; " align="left"><code class="literal">defaultValue</code></td><td style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; ">Sets the value of the <code class="literal">defaultValue</code>
                descriptor field</td><td style="border-bottom: 1.0pt solid ; "><code class="classname">ManagedAttribute</code></td></tr><tr><td style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; " align="left"><code class="literal">log</code></td><td style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; ">Sets the value of the <code class="literal">log</code> descriptor
                field</td><td style="border-bottom: 1.0pt solid ; "><code class="classname">ManagedResource</code></td></tr><tr><td style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; " align="left"><code class="literal">logFile</code></td><td style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; ">Sets the value of the <code class="literal">logFile</code>
                descriptor field</td><td style="border-bottom: 1.0pt solid ; "><code class="classname">ManagedResource</code></td></tr><tr><td style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; " align="left"><code class="literal">persistPolicy</code></td><td style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; ">Sets the value of the <code class="literal">persistPolicy</code>
                descriptor field</td><td style="border-bottom: 1.0pt solid ; "><code class="classname">ManagedResource</code></td></tr><tr><td style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; " align="left"><code class="literal">persistPeriod</code></td><td style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; ">Sets the value of the <code class="literal">persistPeriod</code>
                descriptor field</td><td style="border-bottom: 1.0pt solid ; "><code class="classname">ManagedResource</code></td></tr><tr><td style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; " align="left"><code class="literal">persistLocation</code></td><td style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; ">Sets the value of the
                <code class="literal">persistLocation</code> descriptor field</td><td style="border-bottom: 1.0pt solid ; "><code class="classname">ManagedResource</code></td></tr><tr><td style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; " align="left"><code class="literal">persistName</code></td><td style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; ">Sets the value of the <code class="literal">persistName</code>
                descriptor field</td><td style="border-bottom: 1.0pt solid ; "><code class="classname">ManagedResource</code></td></tr><tr><td style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; " align="left"><code class="literal">name</code></td><td style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; ">Sets the display name of an operation parameter</td><td style="border-bottom: 1.0pt solid ; "><code class="literal">ManagedOperationParameter</code></td></tr><tr><td style="border-right: 1.0pt solid ; " align="left"><code class="literal">index</code></td><td style="border-right: 1.0pt solid ; ">Sets the index of an operation parameter</td><td style=""><code class="literal">ManagedOperationParameter</code></td></tr></tbody></table></div></div><p><br class="table-break"></p></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="jmx-interface-autodetect"></a>22.3.4&nbsp;The <code class="classname">AutodetectCapableMBeanInfoAssembler</code>
      interface</h3></div></div></div><p>To simplify configuration even further, Spring introduces the
      <code class="classname">AutodetectCapableMBeanInfoAssembler</code> interface
      which extends the <code class="interfacename">MBeanInfoAssembler</code>
      interface to add support for autodetection of MBean resources. If you
      configure the <code class="classname">MBeanExporter</code> with an instance of
      <code class="classname">AutodetectCapableMBeanInfoAssembler</code> then it is
      allowed to "vote" on the inclusion of beans for exposure to JMX.</p><p>Out of the box, the only implementation of the
      <code class="classname">AutodetectCapableMBeanInfo</code> interface is the
      <code class="classname">MetadataMBeanInfoAssembler</code> which will vote to
      include any bean which is marked with the
      <code class="classname">ManagedResource</code> attribute. The default approach
      in this case is to use the bean name as the
      <code class="classname">ObjectName</code> which results in a configuration like
      this:</p><pre class="programlisting">&lt;<span class="hl-tag">beans</span>&gt;

  &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"exporter"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.export.MBeanExporter"</span>&gt;
    &lt;<span class="hl-comment">!-- notice how no 'beans' are explicitly configured here --</span>&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"autodetect"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"true"</span>/&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"assembler"</span> <span class="hl-attribute">ref</span>=<span class="hl-value">"assembler"</span>/&gt;
  &lt;<span class="hl-tag">/bean</span>&gt;

  &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"testBean"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.JmxTestBean"</span>&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"name"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"TEST"</span>/&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"age"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"100"</span>/&gt;
  &lt;<span class="hl-tag">/bean</span>&gt;

  &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"assembler"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler"</span>&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"attributeSource"</span>&gt;
        &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource"</span>/&gt;
    &lt;<span class="hl-tag">/property</span>&gt;
  &lt;<span class="hl-tag">/bean</span>&gt;

&lt;<span class="hl-tag">/beans</span>&gt;</pre><p>Notice that in this configuration no beans are passed to the
      <code class="classname">MBeanExporter</code>; however, the
      <code class="classname">JmxTestBean</code> will still be registered since it is
      marked with the <code class="classname">ManagedResource</code> attribute and the
      <code class="classname">MetadataMBeanInfoAssembler</code> detects this and votes
      to include it. The only problem with this approach is that the name of
      the <code class="classname">JmxTestBean</code> now has business meaning. You can
      address this issue by changing the default behavior for
      <code class="classname">ObjectName</code> creation as defined in
      <a class="xref" href="jmx.html#jmx-naming" title="22.4&nbsp;Controlling the ObjectNames for your beans">Section&nbsp;22.4, &#8220;Controlling the ObjectNames for your beans&#8221;</a>.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="jmx-interface-java"></a>22.3.5&nbsp;Defining management interfaces using Java interfaces</h3></div></div></div><p>In addition to the
      <code class="classname">MetadataMBeanInfoAssembler</code>, Spring also includes
      the <code class="classname">InterfaceBasedMBeanInfoAssembler</code> which allows
      you to constrain the methods and properties that are exposed based on
      the set of methods defined in a collection of interfaces.</p><p>Although the standard mechanism for exposing MBeans is to use
      interfaces and a simple naming scheme, the
      <code class="classname">InterfaceBasedMBeanInfoAssembler</code> extends this
      functionality by removing the need for naming conventions, allowing you
      to use more than one interface and removing the need for your beans to
      implement the MBean interfaces.</p><p>Consider this interface that is used to define a management
      interface for the <code class="classname">JmxTestBean</code> class that you saw
      earlier:</p><pre class="programlisting"><span class="hl-keyword">public</span> <span class="hl-keyword">interface</span> IJmxTestBean {

  <span class="hl-keyword">public</span> <span class="hl-keyword">int</span> add(<span class="hl-keyword">int</span> x, <span class="hl-keyword">int</span> y);

  <span class="hl-keyword">public</span> <span class="hl-keyword">long</span> myOperation();

  <span class="hl-keyword">public</span> <span class="hl-keyword">int</span> getAge();

  <span class="hl-keyword">public</span> <span class="hl-keyword">void</span> setAge(<span class="hl-keyword">int</span> age);

  <span class="hl-keyword">public</span> <span class="hl-keyword">void</span> setName(String name);

  <span class="hl-keyword">public</span> String getName();
}</pre><p>This interface defines the methods and properties that will be
      exposed as operations and attributes on the JMX MBean. The code below
      shows how to configure Spring JMX to use this interface as the
      definition for the management interface:</p><pre class="programlisting">&lt;<span class="hl-tag">beans</span>&gt;

  &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"exporter"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.export.MBeanExporter"</span>&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"beans"</span>&gt;
      &lt;<span class="hl-tag">map</span>&gt;
        &lt;<span class="hl-tag">entry</span> <span class="hl-attribute">key</span>=<span class="hl-value">"bean:name=testBean5"</span> <span class="hl-attribute">value-ref</span>=<span class="hl-value">"testBean"</span>/&gt;
      &lt;<span class="hl-tag">/map</span>&gt;
    &lt;<span class="hl-tag">/property</span>&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"assembler"</span>&gt;
      &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.export.assembler.InterfaceBasedMBeanInfoAssembler"</span>&gt;
        &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"managedInterfaces"</span>&gt;
          &lt;<span class="hl-tag">value</span>&gt;org.springframework.jmx.IJmxTestBean&lt;<span class="hl-tag">/value</span>&gt;
        &lt;<span class="hl-tag">/property</span>&gt;
      &lt;<span class="hl-tag">/bean</span>&gt;
    &lt;<span class="hl-tag">/property</span>&gt;
  &lt;<span class="hl-tag">/bean</span>&gt;

  &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"testBean"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.JmxTestBean"</span>&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"name"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"TEST"</span>/&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"age"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"100"</span>/&gt;
  &lt;<span class="hl-tag">/bean</span>&gt;

&lt;<span class="hl-tag">/beans</span>&gt;</pre><p>Here you can see that the
      <code class="classname">InterfaceBasedMBeanInfoAssembler</code> is configured to
      use the <code class="interfacename">IJmxTestBean</code> interface when
      constructing the management interface for any bean. It is important to
      understand that beans processed by the
      <code class="classname">InterfaceBasedMBeanInfoAssembler</code> are
      <span class="emphasis"><em>not</em></span> required to implement the interface used to
      generate the JMX management interface.</p><p>In the case above, the <code class="interfacename">IJmxTestBean</code>
      interface is used to construct all management interfaces for all beans.
      In many cases this is not the desired behavior and you may want to use
      different interfaces for different beans. In this case, you can pass
      <code class="classname">InterfaceBasedMBeanInfoAssembler</code> a
      <code class="classname">Properties</code> instance via the
      <code class="literal">interfaceMappings</code> property, where the key of each
      entry is the bean name and the value of each entry is a comma-separated
      list of interface names to use for that bean.</p><p>If no management interface is specified through either the
      <code class="literal">managedInterfaces</code> or
      <code class="literal">interfaceMappings</code> properties, then the
      <code class="classname">InterfaceBasedMBeanInfoAssembler</code> will reflect on
      the bean and use all of the interfaces implemented by that bean to
      create the management interface.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="jmx-interface-methodnames"></a>22.3.6&nbsp;Using
      <code class="classname">MethodNameBasedMBeanInfoAssembler</code></h3></div></div></div><p>The <code class="classname">MethodNameBasedMBeanInfoAssembler</code>
      allows you to specify a list of method names that will be exposed to JMX
      as attributes and operations. The code below shows a sample
      configuration for this:</p><pre class="programlisting">&lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"exporter"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.export.MBeanExporter"</span>&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"beans"</span>&gt;
      &lt;<span class="hl-tag">map</span>&gt;
        &lt;<span class="hl-tag">entry</span> <span class="hl-attribute">key</span>=<span class="hl-value">"bean:name=testBean5"</span> <span class="hl-attribute">value-ref</span>=<span class="hl-value">"testBean"</span>/&gt;
      &lt;<span class="hl-tag">/map</span>&gt;
    &lt;<span class="hl-tag">/property</span>&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"assembler"</span>&gt;
      &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.export.assembler.MethodNameBasedMBeanInfoAssembler"</span>&gt;
        &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"managedMethods"</span>&gt;
          &lt;<span class="hl-tag">value</span>&gt;add,myOperation,getName,setName,getAge&lt;<span class="hl-tag">/value</span>&gt;
        &lt;<span class="hl-tag">/property</span>&gt;
      &lt;<span class="hl-tag">/bean</span>&gt;
    &lt;<span class="hl-tag">/property</span>&gt;
&lt;<span class="hl-tag">/bean</span>&gt;</pre><p>Here you can see that the methods <code class="literal">add</code> and
      <code class="literal">myOperation</code> will be exposed as JMX operations and
      <code class="literal">getName()</code>, <code class="literal">setName(String)</code> and
      <code class="literal">getAge()</code> will be exposed as the appropriate half of a
      JMX attribute. In the code above, the method mappings apply to beans
      that are exposed to JMX. To control method exposure on a bean-by-bean
      basis, use the <code class="literal">methodMappings</code> property of
      <code class="classname">MethodNameMBeanInfoAssembler</code> to map bean names to
      lists of method names.</p></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="jmx-naming"></a>22.4&nbsp;Controlling the <code class="classname">ObjectName</code>s for your beans</h2></div></div></div><p>Behind the scenes, the <code class="classname">MBeanExporter</code>
    delegates to an implementation of the
    <code class="classname">ObjectNamingStrategy</code> to obtain
    <code class="classname">ObjectName</code>s for each of the beans it is
    registering. The default implementation,
    <code class="classname">KeyNamingStrategy</code>, will, by default, use the key of
    the <code class="literal">beans</code> <code class="interfacename">Map</code> as the
    <code class="classname">ObjectName</code>. In addition, the
    <code class="classname">KeyNamingStrategy</code> can map the key of the
    <code class="literal">beans</code> <code class="interfacename">Map</code> to an entry in a
    <code class="classname">Properties</code> file (or files) to resolve the
    <code class="classname">ObjectName</code>. In addition to the
    <code class="classname">KeyNamingStrategy</code>, Spring provides two additional
    <code class="classname">ObjectNamingStrategy</code> implementations: the
    <code class="classname">IdentityNamingStrategy</code> that builds an
    <code class="classname">ObjectName</code> based on the JVM identity of the bean
    and the <code class="classname">MetadataNamingStrategy</code> that uses source
    level metadata to obtain the <code class="classname">ObjectName</code>.</p><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="jmx-naming-properties"></a>22.4.1&nbsp;Reading <code class="classname">ObjectName</code>s from <code class="classname">Properties</code></h3></div></div></div><p>You can configure your own
      <code class="classname">KeyNamingStrategy</code> instance and configure it to
      read <code class="classname">ObjectName</code>s from a
      <code class="classname">Properties</code> instance rather than use bean key. The
      <code class="classname">KeyNamingStrategy</code> will attempt to locate an entry
      in the <code class="classname">Properties</code> with a key corresponding to the
      bean key. If no entry is found or if the
      <code class="classname">Properties</code> instance is <code class="literal">null</code>
      then the bean key itself is used.</p><p>The code below shows a sample configuration for the
      <code class="classname">KeyNamingStrategy</code>:</p><pre class="programlisting">&lt;<span class="hl-tag">beans</span>&gt;

  &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"exporter"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.export.MBeanExporter"</span>&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"beans"</span>&gt;
      &lt;<span class="hl-tag">map</span>&gt;
        &lt;<span class="hl-tag">entry</span> <span class="hl-attribute">key</span>=<span class="hl-value">"testBean"</span> <span class="hl-attribute">value-ref</span>=<span class="hl-value">"testBean"</span>/&gt;
      &lt;<span class="hl-tag">/map</span>&gt;
    &lt;<span class="hl-tag">/property</span>&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"namingStrategy"</span> <span class="hl-attribute">ref</span>=<span class="hl-value">"namingStrategy"</span>/&gt;
  &lt;<span class="hl-tag">/bean</span>&gt;

  &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"testBean"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.JmxTestBean"</span>&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"name"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"TEST"</span>/&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"age"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"100"</span>/&gt;
  &lt;<span class="hl-tag">/bean</span>&gt;

  &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"namingStrategy"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.export.naming.KeyNamingStrategy"</span>&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"mappings"</span>&gt;
      &lt;<span class="hl-tag">props</span>&gt;
        &lt;<span class="hl-tag">prop</span> <span class="hl-attribute">key</span>=<span class="hl-value">"testBean"</span>&gt;bean:name=testBean1&lt;<span class="hl-tag">/prop</span>&gt;
      &lt;<span class="hl-tag">/props</span>&gt;
    &lt;<span class="hl-tag">/property</span>&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"mappingLocations"</span>&gt;
      &lt;<span class="hl-tag">value</span>&gt;names1.properties,names2.properties&lt;<span class="hl-tag">/value</span>&gt;
    &lt;<span class="hl-tag">/property</span>&gt;
  &lt;<span class="hl-tag">/bean

&lt;/beans</span>&gt;</pre><p>Here an instance of <code class="classname">KeyNamingStrategy</code> is
      configured with a <code class="classname">Properties</code> instance that is
      merged from the <code class="classname">Properties</code> instance defined by
      the mapping property and the properties files located in the paths
      defined by the mappings property. In this configuration, the
      <code class="literal">testBean</code> bean will be given the
      <code class="classname">ObjectName</code> <code class="literal">bean:name=testBean1</code>
      since this is the entry in the <code class="classname">Properties</code>
      instance that has a key corresponding to the bean key.</p><p>If no entry in the <code class="classname">Properties</code> instance can
      be found then the bean key name is used as the
      <code class="classname">ObjectName</code>.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="jmx-naming-metadata"></a>22.4.2&nbsp;Using the <code class="classname">MetadataNamingStrategy</code></h3></div></div></div><p>The <code class="classname">MetadataNamingStrategy</code> uses
      the <code class="literal">objectName</code> property of the
      <code class="classname">ManagedResource</code> attribute on each bean to create
      the <code class="classname">ObjectName</code>. The code below shows the
      configuration for the
      <code class="classname">MetadataNamingStrategy</code>:</p><pre class="programlisting">&lt;<span class="hl-tag">beans</span>&gt;

  &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"exporter"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.export.MBeanExporter"</span>&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"beans"</span>&gt;
      &lt;<span class="hl-tag">map</span>&gt;
        &lt;<span class="hl-tag">entry</span> <span class="hl-attribute">key</span>=<span class="hl-value">"testBean"</span> <span class="hl-attribute">value-ref</span>=<span class="hl-value">"testBean"</span>/&gt;
      &lt;<span class="hl-tag">/map</span>&gt;
    &lt;<span class="hl-tag">/property</span>&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"namingStrategy"</span> <span class="hl-attribute">ref</span>=<span class="hl-value">"namingStrategy"</span>/&gt;
  &lt;<span class="hl-tag">/bean</span>&gt;

  &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"testBean"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.JmxTestBean"</span>&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"name"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"TEST"</span>/&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"age"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"100"</span>/&gt;
  &lt;<span class="hl-tag">/bean</span>&gt;

  &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"namingStrategy"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.export.naming.MetadataNamingStrategy"</span>&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"attributeSource"</span> <span class="hl-attribute">ref</span>=<span class="hl-value">"attributeSource"</span>/&gt;
  &lt;<span class="hl-tag">/bean</span>&gt;

  &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"attributeSource"</span>
      <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.export.metadata.AttributesJmxAttributeSource"</span>/&gt;

&lt;<span class="hl-tag">/beans</span>&gt;</pre><p>If no <code class="literal">objectName</code> has been provided for
      the <code class="classname">ManagedResource</code> attribute, then an
      <code class="classname">ObjectName</code> will be created with the
      following format:
      <span class="emphasis"><em>[fully-qualified-package-name]:type=[short-classname],name=[bean-name]</em></span>.
      For example, the generated <code class="classname">ObjectName</code> for the
      following bean would be: <span class="emphasis"><em>com.foo:type=MyClass,name=myBean</em></span>.
      </p><pre class="programlisting">&lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"myBean"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"com.foo.MyClass"</span>/&gt;</pre></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="jmx-context-mbeanexport"></a>22.4.3&nbsp;The <code class="literal">&lt;context:mbean-export/&gt;</code> element</h3></div></div></div><p>If you are using at least Java 5, then a convenience subclass of
      <code class="classname">MBeanExporter</code> is available:
      <code class="classname">AnnotationMBeanExporter</code>.
      When defining an instance of this subclass, the <code class="literal">namingStrategy</code>,
      <code class="literal">assembler</code>, and <code class="literal">attributeSource</code>
      configuration is no longer needed, since it will always use standard Java
      annotation-based metadata (autodetection is always enabled as well). In fact,
      an even simpler syntax is supported by Spring's
      '<code class="literal">context</code>' namespace.. Rather than defining an
      <code class="classname">MBeanExporter</code> bean, just provide this single element:</p><pre class="programlisting">&lt;<span class="hl-tag">context:mbean-export</span>/&gt;</pre><p>You can provide a reference to a particular MBean server if
      necessary, and the <code class="literal">defaultDomain</code> attribute
      (a property of <code class="classname">AnnotationMBeanExporter</code>)
      accepts an alternate value for the generated MBean
      <code class="classname">ObjectNames</code>' domains. This would be used
      in place of the fully qualified package name as described in the
      previous section on
      <a class="link" href="jmx.html#jmx-naming-metadata" title="22.4.2&nbsp;Using the MetadataNamingStrategy"><code class="classname">MetadataNamingStrategy</code></a>.
      </p><pre class="programlisting">&lt;<span class="hl-tag">context:mbean-export</span> <span class="hl-attribute">server</span>=<span class="hl-value">"myMBeanServer"</span> <span class="hl-attribute">default-domain</span>=<span class="hl-value">"myDomain"</span>/&gt;</pre>.

      <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.gif"></td><th align="left">Note</th></tr><tr><td align="left" valign="top"><p>Do not use interface-based AOP proxies in combination with autodetection of
        JMX annotations in your bean classes. Interface-based proxies 'hide' the target class,
        which also hides the JMX managed resource annotations. Hence, use target-class proxies
        in that case: through setting the 'proxy-target-class' flag on <code class="literal">&lt;aop:config/&gt;</code>,
        <code class="literal">&lt;tx:annotation-driven/&gt;</code>, etc. Otherwise, your JMX beans
        might be silently ignored at startup...</p></td></tr></table></div></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="jmx-jsr160"></a>22.5&nbsp;JSR-160 Connectors</h2></div></div></div><p>For remote access, Spring JMX module offers two
    <code class="classname">FactoryBean</code> implementations inside the
    <code class="literal">org.springframework.jmx.support</code> package for creating
    both server- and client-side connectors.</p><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="jmx-jsr160-server"></a>22.5.1&nbsp;Server-side Connectors</h3></div></div></div><p>To have Spring JMX create, start and expose a JSR-160
      <code class="classname">JMXConnectorServer</code> use the following
      configuration:</p><pre class="programlisting">&lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"serverConnector"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.support.ConnectorServerFactoryBean"</span>/&gt;</pre><p>By default <code class="literal">ConnectorServerFactoryBean</code> creates a
      <code class="classname">JMXConnectorServer</code> bound to
      <code class="literal">"service:jmx:jmxmp://localhost:9875"</code>. The
      <code class="literal">serverConnector</code> bean thus exposes the local
      <code class="interfacename">MBeanServer</code> to clients through the JMXMP protocol
      on localhost, port 9875. Note that the JMXMP protocol is marked as
      optional by the JSR 160 specification: currently, the main open-source
      JMX implementation, MX4J, and the one provided with J2SE 5.0 do
      <span class="emphasis"><em>not</em></span> support JMXMP.</p><p>To specify another URL and register the
      <code class="classname">JMXConnectorServer</code> itself with the
      <code class="interfacename">MBeanServer</code> use the <code class="literal">serviceUrl</code>
      and <code class="classname">ObjectName</code> properties respectively:</p><pre class="programlisting">&lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"serverConnector"</span>
      <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.support.ConnectorServerFactoryBean"</span>&gt;
  &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"objectName"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"connector:name=rmi"</span>/&gt;
  &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"serviceUrl"</span> 
            <span class="hl-attribute">value</span>=<span class="hl-value">"service:jmx:rmi://localhost/jndi/rmi://localhost:1099/myconnector"</span>/&gt;
&lt;<span class="hl-tag">/bean</span>&gt;</pre><p>If the <code class="classname">ObjectName</code> property is set Spring
      will automatically register your connector with the
      <code class="interfacename">MBeanServer</code> under that
      <code class="classname">ObjectName</code>. The example below shows the full set
      of parameters which you can pass to the
      <code class="classname">ConnectorServerFactoryBean</code> when creating a
      JMXConnector:</p><pre class="programlisting">&lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"serverConnector"</span>
      <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.support.ConnectorServerFactoryBean"</span>&gt;
  &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"objectName"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"connector:name=iiop"</span>/&gt;
  &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"serviceUrl"</span> 
               <span class="hl-attribute">value</span>=<span class="hl-value">"service:jmx:iiop://localhost/jndi/iiop://localhost:900/myconnector"</span>/&gt;
  &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"threaded"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"true"</span>/&gt;
  &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"daemon"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"true"</span>/&gt;
  &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"environment"</span>&gt;
    &lt;<span class="hl-tag">map</span>&gt;
      &lt;<span class="hl-tag">entry</span> <span class="hl-attribute">key</span>=<span class="hl-value">"someKey"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"someValue"</span>/&gt;
    &lt;<span class="hl-tag">/map</span>&gt;
  &lt;<span class="hl-tag">/property</span>&gt;
&lt;<span class="hl-tag">/bean</span>&gt;</pre><p>Note that when using a RMI-based connector you need the lookup
      service (tnameserv or rmiregistry) to be started in order for the name
      registration to complete. If you are using Spring to export remote
      services for you via RMI, then Spring will already have constructed an
      RMI registry. If not, you can easily start a registry using the
      following snippet of configuration:</p><pre class="programlisting">&lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"registry"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.remoting.rmi.RmiRegistryFactoryBean"</span>&gt;
  &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"port"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"1099"</span>/&gt;
&lt;<span class="hl-tag">/bean</span>&gt;</pre></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="jmx-jsr160-client"></a>22.5.2&nbsp;Client-side Connectors</h3></div></div></div><p>To create an <code class="classname">MBeanServerConnection</code> to a
      remote JSR-160 enabled <code class="interfacename">MBeanServer</code> use the
      <code class="classname">MBeanServerConnectionFactoryBean</code> as shown
      below:</p><pre class="programlisting">&lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"clientConnector"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.support.MBeanServerConnectionFactoryBean"</span>&gt;
  &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"serviceUrl"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"service:jmx:rmi://localhost:9875"</span>/&gt;
&lt;<span class="hl-tag">/bean</span>&gt;</pre></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="jmx-jsr160-protocols"></a>22.5.3&nbsp;JMX over Burlap/Hessian/SOAP</h3></div></div></div><p>JSR-160 permits extensions to the way in which communication is
      done between the client and the server. The examples above are using the
      mandatory RMI-based implementation required by the JSR-160 specification
      (IIOP and JRMP) and the (optional) JMXMP. By using other providers or
      JMX implementations (such as <a class="ulink" href="http://mx4j.sourceforge.net" target="_top">MX4J</a>) you can take advantage
      of protocols like SOAP, Hessian, Burlap over simple HTTP or SSL and
      others:</p><pre class="programlisting">&lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"serverConnector"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.support.ConnectorServerFactoryBean"</span>&gt;
  &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"objectName"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"connector:name=burlap"</span>/&gt;
  &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"serviceUrl"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"service:jmx:burlap://localhost:9874"</span>/&gt;
&lt;<span class="hl-tag">/bean</span>&gt;</pre><p>In the case of the above example, MX4J 3.0.0 was used; see the
      official MX4J documentation for more information.</p></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="jmx-proxy"></a>22.6&nbsp;Accessing MBeans via Proxies</h2></div></div></div><p>Spring JMX allows you to create proxies that re-route calls to
    MBeans registered in a local or remote <code class="interfacename">MBeanServer</code>.
    These proxies provide you with a standard Java interface through which you
    can interact with your MBeans. The code below shows how to configure a
    proxy for an MBean running in a local
    <code class="interfacename">MBeanServer</code>:</p><pre class="programlisting">&lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"proxy"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.access.MBeanProxyFactoryBean"</span>&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"objectName"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"bean:name=testBean"</span>/&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"proxyInterface"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"org.springframework.jmx.IJmxTestBean"</span>/&gt;
&lt;<span class="hl-tag">/bean</span>&gt;</pre><p>Here you can see that a proxy is created for the MBean registered
    under the <code class="classname">ObjectName</code>:
    <code class="literal">bean:name=testBean</code>. The set of interfaces that the
    proxy will implement is controlled by the
    <code class="literal">proxyInterfaces</code> property and the rules for mapping
    methods and properties on these interfaces to operations and attributes on
    the MBean are the same rules used by the
    <code class="classname">InterfaceBasedMBeanInfoAssembler</code>.</p><p>The <code class="classname">MBeanProxyFactoryBean</code> can create a proxy
    to any MBean that is accessible via an
    <code class="classname">MBeanServerConnection</code>. By default, the local
    <code class="interfacename">MBeanServer</code> is located and used, but you can
    override this and provide an <code class="classname">MBeanServerConnection</code>
    pointing to a remote <code class="interfacename">MBeanServer</code> to cater for
    proxies pointing to remote MBeans:</p><pre class="programlisting">&lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"clientConnector"</span>
      <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.support.MBeanServerConnectionFactoryBean"</span>&gt;
  &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"serviceUrl"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"service:jmx:rmi://remotehost:9875"</span>/&gt;
&lt;<span class="hl-tag">/bean</span>&gt;

&lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"proxy"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.access.MBeanProxyFactoryBean"</span>&gt;
  &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"objectName"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"bean:name=testBean"</span>/&gt;
  &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"proxyInterface"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"org.springframework.jmx.IJmxTestBean"</span>/&gt;
  &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"server"</span> <span class="hl-attribute">ref</span>=<span class="hl-value">"clientConnector"</span>/&gt;
&lt;<span class="hl-tag">/bean</span>&gt;</pre><p>Here you can see that we create an
    <code class="classname">MBeanServerConnection</code> pointing to a remote machine
    using the <code class="classname">MBeanServerConnectionFactoryBean</code>. This
    <code class="classname">MBeanServerConnection</code> is then passed to the
    <code class="classname">MBeanProxyFactoryBean</code> via the
    <code class="literal">server</code> property. The proxy that is created will forward
    all invocations to the <code class="interfacename">MBeanServer</code> via this
    <code class="classname">MBeanServerConnection</code>.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="jmx-notifications"></a>22.7&nbsp;Notifications</h2></div></div></div><p>Spring's JMX offering includes comprehensive support for JMX
    notifications.</p><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="jmx-notifications-listeners"></a>22.7.1&nbsp;Registering Listeners for Notifications</h3></div></div></div><p>Spring's JMX support makes it very easy to register any number of
      <code class="classname">NotificationListeners</code> with any number of MBeans
      (this includes MBeans exported by Spring's
      <code class="classname">MBeanExporter</code> and MBeans registered via some
      other mechanism). By way of an example, consider the scenario where one
	  would like to be informed (via a <code class="classname">Notification</code>)
	  each and every time an attribute of a target MBean changes.</p><pre class="programlisting"><span class="hl-keyword">package</span> com.example;

<span class="hl-keyword">import</span> javax.management.AttributeChangeNotification;
<span class="hl-keyword">import</span> javax.management.Notification;
<span class="hl-keyword">import</span> javax.management.NotificationFilter;
<span class="hl-keyword">import</span> javax.management.NotificationListener;

<span class="hl-keyword">public</span> <span class="hl-keyword">class</span> ConsoleLoggingNotificationListener
               <span class="hl-keyword">implements</span> NotificationListener, NotificationFilter {

    <span class="hl-keyword">public</span> <span class="hl-keyword">void</span> handleNotification(Notification notification, Object handback) {
        System.out.println(notification);
        System.out.println(handback);
    }

    <span class="hl-keyword">public</span> <span class="hl-keyword">boolean</span> isNotificationEnabled(Notification notification) {
        <span class="hl-keyword">return</span> AttributeChangeNotification.<span class="hl-keyword">class</span>.isAssignableFrom(notification.getClass());
    }
}</pre><pre class="programlisting">&lt;<span class="hl-tag">beans</span>&gt;

  &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"exporter"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.export.MBeanExporter"</span>&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"beans"</span>&gt;
      &lt;<span class="hl-tag">map</span>&gt;
        &lt;<span class="hl-tag">entry</span> <span class="hl-attribute">key</span>=<span class="hl-value">"bean:name=testBean1"</span> <span class="hl-attribute">value-ref</span>=<span class="hl-value">"testBean"</span>/&gt;
      &lt;<span class="hl-tag">/map</span>&gt;
    &lt;<span class="hl-tag">/property</span>&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"notificationListenerMappings"</span>&gt;
      &lt;<span class="hl-tag">map</span>&gt;
        &lt;<span class="hl-tag">entry</span> <span class="hl-attribute">key</span>=<span class="hl-value">"bean:name=testBean1"</span>&gt;
          &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">class</span>=<span class="hl-value">"com.example.ConsoleLoggingNotificationListener"</span>/&gt;
        &lt;<span class="hl-tag">/entry</span>&gt;
      &lt;<span class="hl-tag">/map</span>&gt;
    &lt;<span class="hl-tag">/property</span>&gt;
  &lt;<span class="hl-tag">/bean</span>&gt;

  &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"testBean"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.JmxTestBean"</span>&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"name"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"TEST"</span>/&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"age"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"100"</span>/&gt;
  &lt;<span class="hl-tag">/bean</span>&gt;

&lt;<span class="hl-tag">/beans</span>&gt;</pre><p>With the above configuration in place, every time a JMX
		<code class="classname">Notification</code> is broadcast from the target MBean
		(<code class="literal">bean:name=testBean1</code>), the
		<code class="classname">ConsoleLoggingNotificationListener</code> bean that was
		registered as a listener via the
		<code class="literal">notificationListenerMappings</code> property will be
		notified. The <code class="classname">ConsoleLoggingNotificationListener</code>
		bean can then take whatever action it deems appropriate in response to
		the <code class="classname">Notification</code>.</p><p>You can also use straight bean names as the link between exported beans
		and listeners:</p><pre class="programlisting">&lt;<span class="hl-tag">beans</span>&gt;

  &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"exporter"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.export.MBeanExporter"</span>&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"beans"</span>&gt;
      &lt;<span class="hl-tag">map</span>&gt;
        &lt;<span class="hl-tag">entry</span> <span class="hl-attribute">key</span>=<span class="hl-value">"bean:name=testBean1"</span> <span class="hl-attribute">value-ref</span>=<span class="hl-value">"testBean"</span>/&gt;
      &lt;<span class="hl-tag">/map</span>&gt;
    &lt;<span class="hl-tag">/property</span>&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"notificationListenerMappings"</span>&gt;
      &lt;<span class="hl-tag">map</span>&gt;
        &lt;<span class="hl-tag">entry</span> <span class="hl-attribute">key</span>=<span class="hl-value">"testBean"</span>&gt;
          &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">class</span>=<span class="hl-value">"com.example.ConsoleLoggingNotificationListener"</span>/&gt;
        &lt;<span class="hl-tag">/entry</span>&gt;
      &lt;<span class="hl-tag">/map</span>&gt;
    &lt;<span class="hl-tag">/property</span>&gt;
  &lt;<span class="hl-tag">/bean</span>&gt;

  &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"testBean"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.JmxTestBean"</span>&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"name"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"TEST"</span>/&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"age"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"100"</span>/&gt;
  &lt;<span class="hl-tag">/bean</span>&gt;

&lt;<span class="hl-tag">/beans</span>&gt;</pre><p>If one wants to register a single <code class="classname">NotificationListener</code>
		instance for all of the beans that the enclosing <code class="classname">MBeanExporter</code>
		is exporting, one can use the special wildcard <code class="literal">'*'</code> (sans quotes)
		as the key for an entry in the <code class="literal">notificationListenerMappings</code>
		property map; for example:</p><pre class="programlisting">&lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"notificationListenerMappings"</span>&gt;
  &lt;<span class="hl-tag">map</span>&gt;
    &lt;<span class="hl-tag">entry</span> <span class="hl-attribute">key</span>=<span class="hl-value">"*"</span>&gt;
      &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">class</span>=<span class="hl-value">"com.example.ConsoleLoggingNotificationListener"</span>/&gt;
    &lt;<span class="hl-tag">/entry</span>&gt;
  &lt;<span class="hl-tag">/map</span>&gt;
&lt;<span class="hl-tag">/property</span>&gt;</pre><p>If one needs to do the inverse (that is, register a number of distinct
      listeners against an MBean), then one has to use the
      <code class="literal">notificationListeners</code> list property instead (and in
      preference to the <code class="literal">notificationListenerMappings</code>
      property). This time, instead of configuring simply a
      <code class="classname">NotificationListener</code> for a single MBean, one
      configures <code class="classname">NotificationListenerBean</code> instances...
      a <code class="classname">NotificationListenerBean</code> encapsulates a
      <code class="classname">NotificationListener</code> and the
      <code class="classname">ObjectName</code> (or
      <code class="classname">ObjectNames</code>) that it is to be registered against
      in an <code class="interfacename">MBeanServer</code>. The
      <code class="classname">NotificationListenerBean</code> also encapsulates a
      number of other properties such as a
      <code class="classname">NotificationFilter</code> and an arbitrary handback
      object that can be used in advanced JMX notification scenarios.</p><p>The configuration when using
      <code class="classname">NotificationListenerBean</code> instances is not wildly
      different to what was presented previously:</p><pre class="programlisting">&lt;<span class="hl-tag">beans</span>&gt;

  &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"exporter"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.export.MBeanExporter"</span>&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"beans"</span>&gt;
      &lt;<span class="hl-tag">map</span>&gt;
        &lt;<span class="hl-tag">entry</span> <span class="hl-attribute">key</span>=<span class="hl-value">"bean:name=testBean1"</span> <span class="hl-attribute">value-ref</span>=<span class="hl-value">"testBean"</span>/&gt;
      &lt;<span class="hl-tag">/map</span>&gt;
    &lt;<span class="hl-tag">/property</span>&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"notificationListeners"</span>&gt;
        &lt;<span class="hl-tag">list</span>&gt;
            &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.export.NotificationListenerBean"</span>&gt;
                &lt;<span class="hl-tag">constructor-arg</span>&gt;
                    &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">class</span>=<span class="hl-value">"com.example.ConsoleLoggingNotificationListener"</span>/&gt;
                &lt;<span class="hl-tag">/constructor-arg</span>&gt;
                &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"mappedObjectNames"</span>&gt;
                    &lt;<span class="hl-tag">list</span>&gt;
                        &lt;<span class="hl-tag">value</span>&gt;bean:name=testBean1&lt;<span class="hl-tag">/value</span>&gt;
                    &lt;<span class="hl-tag">/list</span>&gt;
                &lt;<span class="hl-tag">/property</span>&gt;
            &lt;<span class="hl-tag">/bean</span>&gt;
        &lt;<span class="hl-tag">/list</span>&gt;
    &lt;<span class="hl-tag">/property</span>&gt;
  &lt;<span class="hl-tag">/bean</span>&gt;

  &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"testBean"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.JmxTestBean"</span>&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"name"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"TEST"</span>/&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"age"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"100"</span>/&gt;
  &lt;<span class="hl-tag">/bean</span>&gt;

&lt;<span class="hl-tag">/beans</span>&gt;</pre><p>The above example is equivalent to the first notification example.
      Lets assume then that we want to be given a handback object every time a
      <code class="classname">Notification</code> is raised, and that additionally we
      want to filter out extraneous <code class="classname">Notifications</code> by
      supplying a <code class="classname">NotificationFilter</code>. (For a full
      discussion of just what a handback object is, and indeed what a
      <code class="classname">NotificationFilter</code> is, please do consult that
      section of the JMX specification (1.2) entitled <code class="literal">'The JMX
      Notification Model'</code>.)</p><pre class="programlisting">&lt;<span class="hl-tag">beans</span>&gt;

  &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"exporter"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.export.MBeanExporter"</span>&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"beans"</span>&gt;
      &lt;<span class="hl-tag">map</span>&gt;
        &lt;<span class="hl-tag">entry</span> <span class="hl-attribute">key</span>=<span class="hl-value">"bean:name=testBean1"</span> <span class="hl-attribute">value-ref</span>=<span class="hl-value">"testBean1"</span>/&gt;
        &lt;<span class="hl-tag">entry</span> <span class="hl-attribute">key</span>=<span class="hl-value">"bean:name=testBean2"</span> <span class="hl-attribute">value-ref</span>=<span class="hl-value">"testBean2"</span>/&gt;
      &lt;<span class="hl-tag">/map</span>&gt;
    &lt;<span class="hl-tag">/property</span>&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"notificationListeners"</span>&gt;
        &lt;<span class="hl-tag">list</span>&gt;
            &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.export.NotificationListenerBean"</span>&gt;
                &lt;<span class="hl-tag">constructor-arg</span> <span class="hl-attribute">ref</span>=<span class="hl-value">"customerNotificationListener"</span>/&gt;
                &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"mappedObjectNames"</span>&gt;
                    &lt;<span class="hl-tag">list</span>&gt;
                        &lt;<span class="hl-comment">!-- handles notifications from two distinct MBeans --</span>&gt;
                        &lt;<span class="hl-tag">value</span>&gt;bean:name=testBean1&lt;<span class="hl-tag">/value</span>&gt;
                        &lt;<span class="hl-tag">value</span>&gt;bean:name=testBean2&lt;<span class="hl-tag">/value</span>&gt;
                    &lt;<span class="hl-tag">/list</span>&gt;
                &lt;<span class="hl-tag">/property</span>&gt;
                &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"handback"</span>&gt;
                    &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">class</span>=<span class="hl-value">"java.lang.String"</span>&gt;
                        &lt;<span class="hl-tag">constructor-arg</span> <span class="hl-attribute">value</span>=<span class="hl-value">"This could be anything..."</span>/&gt;
                    &lt;<span class="hl-tag">/bean</span>&gt;
                &lt;<span class="hl-tag">/property</span>&gt;
                &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"notificationFilter"</span> <span class="hl-attribute">ref</span>=<span class="hl-value">"customerNotificationListener"</span>/&gt;
            &lt;<span class="hl-tag">/bean</span>&gt;
        &lt;<span class="hl-tag">/list</span>&gt;
    &lt;<span class="hl-tag">/property</span>&gt;
  &lt;<span class="hl-tag">/bean</span>&gt;
  
  &lt;<span class="hl-comment">!-- implements both the NotificationListener and NotificationFilter interfaces --</span>&gt;
  &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"customerNotificationListener"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"com.example.ConsoleLoggingNotificationListener"</span>/&gt;

  &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"testBean1"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.JmxTestBean"</span>&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"name"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"TEST"</span>/&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"age"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"100"</span>/&gt;
  &lt;<span class="hl-tag">/bean</span>&gt;

  &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"testBean2"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.jmx.JmxTestBean"</span>&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"name"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"ANOTHER TEST"</span>/&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"age"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"200"</span>/&gt;
  &lt;<span class="hl-tag">/bean</span>&gt;

&lt;<span class="hl-tag">/beans</span>&gt;</pre></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="jmx-notifications-publishing"></a>22.7.2&nbsp;Publishing Notifications</h3></div></div></div><p>Spring provides support not just for registering to receive
      <code class="classname">Notifications</code>, but also for publishing
      <code class="classname">Notifications</code>.</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.gif"></td><th align="left">Note</th></tr><tr><td align="left" valign="top"><p>Please note that this section is really only relevant to Spring
        managed beans that have been exposed as MBeans via an
        <code class="classname">MBeanExporter</code>; any existing, user-defined
        MBeans should use the standard JMX APIs for notification publication.</p></td></tr></table></div><p>The key interface in Spring's JMX notification publication support
      is the <code class="classname">NotificationPublisher</code> interface (defined
      in the <code class="literal">org.springframework.jmx.export.notification</code>
      package). Any bean that is going to be exported as an MBean via an
      <code class="classname">MBeanExporter</code> instance can implement the related
      <code class="classname">NotificationPublisherAware</code> interface to gain
      access to a <code class="classname">NotificationPublisher</code> instance. The
      <code class="classname">NotificationPublisherAware</code> interface simply
      supplies an instance of a <code class="classname">NotificationPublisher</code>
      to the implementing bean via a simple setter method, which the bean can
      then use to publish <code class="classname">Notifications</code>.</p><p>As stated in the Javadoc for the
      <code class="classname">NotificationPublisher</code> class, managed beans that
      are publishing events via the
      <code class="classname">NotificationPublisher</code> mechanism are
      <span class="emphasis"><em>not</em></span> responsible for the state management of any
      notification listeners and the like ... Spring's JMX support will take
      care of handling all the JMX infrastructure issues. All one need do as
      an application developer is implement the
      <code class="classname">NotificationPublisherAware</code> interface and start
      publishing events using the supplied
      <code class="classname">NotificationPublisher</code> instance. Note that the
      <code class="classname">NotificationPublisher</code> will be set
      <span class="emphasis"><em>after</em></span> the managed bean has been registered with an
      <code class="interfacename">MBeanServer</code>.</p><p>Using a <code class="classname">NotificationPublisher</code> instance is
      quite straightforward... one simply creates a JMX
      <code class="classname">Notification</code> instance (or an instance of an
      appropriate <code class="classname">Notification</code> subclass), populates
      the notification with the data pertinent to the event that is to be
      published, and one then invokes the
      <code class="methodname">sendNotification(Notification)</code> on the
      <code class="classname">NotificationPublisher</code> instance, passing in the
      <code class="classname">Notification</code>.</p><p>Find below a simple example... in this scenario, exported
      instances of the <code class="classname">JmxTestBean</code> are going to publish
      a <code class="classname">NotificationEvent</code> every time the
      <code class="literal">add(int, int)</code> operation is invoked.</p><pre class="programlisting"><span class="hl-keyword">package</span> org.springframework.jmx;
			
<span class="hl-keyword">import</span> org.springframework.jmx.export.notification.NotificationPublisherAware;
<span class="hl-keyword">import</span> org.springframework.jmx.export.notification.NotificationPublisher;
<span class="hl-keyword">import</span> javax.management.Notification;

<span class="hl-keyword">public</span> <span class="hl-keyword">class</span> JmxTestBean <span class="hl-keyword">implements</span> IJmxTestBean, NotificationPublisherAware {

    <span class="hl-keyword">private</span> String name;
    <span class="hl-keyword">private</span> <span class="hl-keyword">int</span> age;
    <span class="hl-keyword">private</span> <span class="hl-keyword">boolean</span> isSuperman;
    <span class="hl-keyword">private</span> NotificationPublisher publisher;

    <span class="hl-comment">// other getters and setters omitted for clarity</span>

    <span class="hl-keyword">public</span> <span class="hl-keyword">int</span> add(<span class="hl-keyword">int</span> x, <span class="hl-keyword">int</span> y) {
        <span class="hl-keyword">int</span> answer = x + y;
        <span class="hl-keyword">this</span>.publisher.sendNotification(<span class="hl-keyword">new</span> Notification(<span class="hl-string">"add"</span>, <span class="hl-keyword">this</span>, 0));
        <span class="hl-keyword">return</span> answer;
    }

    <span class="hl-keyword">public</span> <span class="hl-keyword">void</span> dontExposeMe() {
        <span class="hl-keyword">throw</span> <span class="hl-keyword">new</span> RuntimeException();
    }
    
    <span class="hl-keyword">public</span> <span class="hl-keyword">void</span> setNotificationPublisher(NotificationPublisher notificationPublisher) {
        <span class="hl-keyword">this</span>.publisher = notificationPublisher;
    }
}</pre><p>The <code class="classname">NotificationPublisher</code> interface and the
      machinery to get it all working is one of the nicer features of Spring's JMX support.
	  It does however come with the price tag of coupling your classes to both Spring and JMX; as
	  always, the advice here is to be pragmatic... if you need the functionality offered by the
      <code class="classname">NotificationPublisher</code> and you can accept the coupling to both Spring
	  and JMX, then do so.</p></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="jmx-resources"></a>22.8&nbsp;Further Resources</h2></div></div></div><p>This section contains links to further resources about JMX.</p><div class="itemizedlist"><ul type="disc"><li><p>The <a class="ulink" href="http://java.sun.com/products/JavaManagement/" target="_top">JMX homepage</a> at Sun</p></li><li><p>The <a class="ulink" href="http://jcp.org/aboutJava/communityprocess/final/jsr003/index3.html" target="_top">JMX specification</a> (JSR-000003)</p></li><li><p>The <a class="ulink" href="http://jcp.org/aboutJava/communityprocess/final/jsr160/index.html" target="_top">JMX Remote API specification</a> (JSR-000160)</p></li><li><p>The <a class="ulink" href="http://mx4j.sourceforge.net/" target="_top">MX4J
        homepage</a> (an Open Source implementation of various JMX
        specs)</p></li><li><p><a class="ulink" href="http://java.sun.com/developer/technicalArticles/J2SE/jmx.html" target="_top">Getting Started with JMX</a> - an introductory article from Sun.</p></li></ul></div></div></div><!--Begin LoopFuse code--><script src="http://loopfuse.net/webrecorder/js/listen.js" type="text/javascript"></script><script type="text/javascript">
			_lf_cid = "LF_48be82fa";
			_lf_remora();
		</script><!--End LoopFuse code--><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="jms.html">Prev</a>&nbsp;</td><td width="20%" align="center"><a accesskey="u" href="spring-integration.html">Up</a></td><td width="40%" align="right">&nbsp;<a accesskey="n" href="cci.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">21.&nbsp;JMS (Java Message Service)&nbsp;</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">&nbsp;23.&nbsp;JCA CCI</td></tr></table></div><!-- Begin 2nd Google code - a mod -->
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));</script>
<script type="text/javascript">
var OrgTracker = _gat._getTracker("UA-2728886-2"); OrgTracker._setDomainName("none");
OrgTracker._setAllowLinker(true);
OrgTracker._trackPageview();
</script>
 <!-- End 2nd Google code -->


<script type="text/javascript">

function detectLastFrame() {
   var thisF = this.window;
   var allF = parent.top.frames;
   return allF[allF.length - 1] == thisF; }

// Only inject the following code if this is a normal page or the last // frame of a frameset.
if (parent.top.frames.length == 0 || detectLastFrame()) {
    // Inject the SpringSource search widget too.
    document.write(unescape("%3Cscript src='http://search.springsource.org/widget/searchtool.js' type='text/javascript'%3E%3C/script%3E"));
    // Inject the Eloqua code
    document.write(unescape("%3Cscript src='/elqNow/elqCfg.js' type='text/javascript'%3E%3C/script%3E"));
    document.write(unescape("%3Cscript src='/elqNow/elqImg.js' type='text/javascript'%3E%3C/script%3E"));

}
</script>
</body>
</html>